// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.reports.editor.design.handlers;

import org.dengues.reports.editor.ReportColorConstants;
import org.eclipse.draw2d.FigureUtilities;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.PrecisionRectangle;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.handles.HandleBounds;
import org.eclipse.gef.handles.MoveHandleLocator;
import org.eclipse.swt.graphics.Image;

/**
 * The class is the table, list grid guide handle
 * 
 * 
 * /** Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-2-22 qiang.zhang $
 * 
 */
public class TableGuideHandle extends AbstractGuideHandle {

    protected Image image;

    protected String indicatorLabel = "  ";//$NON-NLS-1$

    private static final Dimension DEFAULT_VALUE = new Dimension(40, 20);

    protected Insets gapInsets = new Insets(3, 3, 3, 3);

    int gap;

    public TableGuideHandle(GraphicalEditPart owner) {
        super(owner, new TableGuideHandleLocator(owner));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.draw2d.Figure#paint(org.eclipse.draw2d.Graphics)
     */
    public void paintFigure(Graphics graphics) {
        Dimension rect = calculateIndicatorDimension(graphics, 1);

        int width = 1;
        if (!getBounds().getSize().equals(rect)) {
            setSize(rect);
        }

        Rectangle bounds = getBounds().getCopy();
        bounds.y = bounds.y + 2;
        graphics.setBackgroundColor(ReportColorConstants.TableGuideFillColor);
        graphics.fillRectangle(bounds);
        graphics.setForegroundColor(ReportColorConstants.ShadowLineColor);
        bounds = getBounds().getCopy();
        graphics.drawLine(bounds.x, bounds.y, bounds.x, bounds.y + bounds.height);
        graphics.drawLine(bounds.x, bounds.y + bounds.height - 1, bounds.x + bounds.width, bounds.y + bounds.height - 1);
        graphics.drawLine(bounds.x + bounds.width - 1, bounds.y, bounds.x + bounds.width - 1, bounds.y + bounds.height);
        graphics.setForegroundColor(ReportColorConstants.TableGuideFillColor);
        graphics.drawLine(bounds.x, bounds.y, bounds.x + bounds.width - 1, bounds.y);
        int x = getBounds().x + gapInsets.left;
        if (image != null) {
            graphics.drawImage(image, x, bounds.y + gapInsets.top);
            x += image.getBounds().width + gap;
        }
        graphics.setForegroundColor(ReportColorConstants.TableGuideTextColor);
        graphics.drawString(indicatorLabel, x + 2 * width, bounds.y + 2 + gapInsets.top - width);
    }

    private static class TableGuideHandleLocator extends MoveHandleLocator {

        private GraphicalEditPart owner;

        /**
         * @param ref
         */
        public TableGuideHandleLocator(GraphicalEditPart part) {
            super(part.getFigure());
            setOwner(part);
        }

        /*
         * Sets the handle the bounds
         * 
         * @see org.eclipse.draw2d.Locator#relocate(org.eclipse.draw2d.IFigure)
         */
        public void relocate(IFigure target) {
            // System.out.println("relocate");
            Rectangle bounds;
            if (getReference() instanceof HandleBounds)
                bounds = ((HandleBounds) getReference()).getHandleBounds();
            else
                bounds = getReference().getBounds();

            bounds = new PrecisionRectangle(new Rectangle(bounds.x, bounds.y + bounds.height, DEFAULT_VALUE.width,
                    DEFAULT_VALUE.height));

            getReference().translateToAbsolute(bounds);
            target.translateToRelative(bounds);
            target.setBounds(bounds);
        }

        public GraphicalEditPart getOwner() {
            return owner;
        }

        public void setOwner(GraphicalEditPart owner) {
            this.owner = owner;
        }
    }

    private Dimension calculateIndicatorDimension(Graphics g, int width) {
        gap = 0;
        Dimension iconDimension = new Dimension();
        if (image != null) {
            iconDimension = new Dimension(image);
            gap = 2;
        }
        Dimension d = FigureUtilities.getTextExtents(indicatorLabel, g.getFont());
        int incheight = 0;
        if (iconDimension.height > d.height) {
            incheight = iconDimension.height - d.height;
        }
        d.expand(iconDimension.width + gap + gapInsets.left + gapInsets.right + 2 * width + 2, incheight + gapInsets.top
                + gapInsets.bottom);

        return d;
    }

    /**
     * Sets the left corner label
     * 
     * @param indicatorLabel
     */
    public void setIndicatorLabel(String indicatorLabel) {
        if (indicatorLabel != null) {
            this.indicatorLabel = indicatorLabel;
        }
    }

    /**
     * Sets the left corner
     * 
     * @param image
     */
    public void setIndicatorIcon(Image image) {
        this.image = image;
    }

}